package com.work.formaldehyde.activity;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.work.formaldehyde.R;
import com.work.formaldehyde.model.UserModel;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class SetZhiFuBaoActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText zfb_name, pp_name;


    private int submit = 0;

    private RelativeLayout submit_user;
    private ImageView comeback;
    private LinearLayout pwd_login, zuida;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ApiUtils.ActivityIsLogin(this, 1);
        setContentView(R.layout.activity_zhifubaoname);
        inti();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        ApiUtils.ActivityIsLogin(this, 1);
    }

    private InputMethodManager imm;
    private boolean isOpen;

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        isOpen = imm.isActive();
        pwd_login = findViewById(R.id.pwd_login);
        pwd_login.setOnClickListener(this);
        pp_name = findViewById(R.id.pp_name);
        comeback = findViewById(R.id.jiance_comeback);
        comeback.setOnClickListener(this);
        zfb_name = findViewById(R.id.zfb_name);
        submit_user = findViewById(R.id.submit_user);
        submit_user.setOnClickListener(this);
        zuida = findViewById(R.id.zuida);
        zuida.setOnClickListener(this);
        zfb_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!charSequence.toString().equals("")) {
                    submit_user.setBackgroundResource(R.drawable.rela_selected_bg_blue);
                    submit = 1;
                } else {
                    submit_user.setBackgroundResource(R.drawable.rela_selected_bg_huise);
                    submit = 0;
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private String phonenum;
    private String codes;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ApiUtils.popupWindows!=null&&ApiUtils.popupWindows.isShowing()){
            return false;
        }
        return super.dispatchTouchEvent(ev);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.pwd_login:
                if (isOpen)
                    imm.hideSoftInputFromWindow(zfb_name.getWindowToken(), 0);
                break;
            case R.id.jiance_comeback:
                onBackPressed();
                break;
            case R.id.zuida:
                if (isOpen) {
                    imm.hideSoftInputFromWindow(zfb_name.getWindowToken(), 0);
                }
                break;
            case R.id.submit_user:
                if (submit == 1) {
                    phonenum = zfb_name.getText().toString();
                    codes = pp_name.getText().toString();
                    if (!Url.USER_ID.equals("")) {
                        if (ApiUtils.isNetworkConnected(SetZhiFuBaoActivity.this)) {
                            if (isOpen)
                                imm.hideSoftInputFromWindow(zfb_name.getWindowToken(), 0);
                            ApiUtils.windows(SetZhiFuBaoActivity.this, submit_user, "正在绑定");
                            new Handler().postDelayed(new Runnable() {
                                public void run() {
                                    ApiUtils.closepopup();
                                }
                            }, 10000);
                            okHttp_postFromParameters(1);
                        } else {
                            ApiUtils.SetToast(SetZhiFuBaoActivity.this, "当前无网络连接");
                        }
                    } else {
                        Intent is_login = new Intent(SetZhiFuBaoActivity.this, LoginActivity.class);
                        startActivity(is_login, ActivityOptions.makeSceneTransitionAnimation(SetZhiFuBaoActivity.this).toBundle());
                        ApiUtils.SetToast(SetZhiFuBaoActivity.this, "请先登录");
                    }
                }
                break;
        }
    }


//    private void okHttp_postFromParameters(final int code) {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    OkHttpClient okHttpClient = new OkHttpClient();
//                    RequestBody formBody = new FormBody.Builder().add("uid", Url.USER_ID).add("zfbzh", phonenum).add("zfbname", codes)
//                            .build();
//                    Request request = new Request.Builder().url(Url.BANGDINGZHIFUBAO).post(formBody).build();
//                    okhttp3.Response response = okHttpClient.newCall(request).execute();
//                    String msg = response.body().string();
//                    JSONObject success_Object = new JSONObject(msg);
//                    String code = success_Object.getString("code");
//                    Message message = new Message();
//                    message.obj = code;
//                    gethot.sendMessage(message);
//                } catch (
//                        Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }).
//                start();
//    }

    private void okHttp_postFromParameters(final int code) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    RequestBody formBody = new FormBody.Builder().add("uid", Url.USER_ID).add("zfbzh", phonenum).add("zfbname", codes).add("logintoken", Url.LOGIN_TOKEN)
                            .build();
                    Request request = new Request.Builder().url(Url.SETALIPAY).post(formBody).build();
                    okhttp3.Response response = okHttpClient.newCall(request).execute();
                    String msg = response.body().string();
                    JSONObject success_Object = new JSONObject(msg);
                    String code = success_Object.getString("code");
                    Message message = new Message();
                    message.obj = code;
                    gethot.sendMessage(message);
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }
    public Handler gethot = new Handler() {
        public void handleMessage(android.os.Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    ApiUtils.closepopup();
                    if (msg.obj.equals("1")) {
                        Url.USER_ALIPAY_NAME = phonenum;
                        onBackPressed();
                        ApiUtils.SetToast(SetZhiFuBaoActivity.this, "绑定成功");
                    } else
                        ApiUtils.SetToast(SetZhiFuBaoActivity.this, "绑定失败");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

}
